package com.zjitc.pojo;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Create By IntelliJ IDEA
 *
 * @author:
 * @create-Time: 2017/11/16  15:24
 */
public class History implements Serializable {
    private Map<String,Product> products = new LinkedHashMap<String, Product>();

    private LinkedList<String> keys = new LinkedList<String>();

    private final int COUNTSIZE = 8;

    public History(Map<String, Product> products) {
        this.products = products;
    }

    public History() {
    }

    public Map<String, Product> getProducts() {
        return products;
    }

    public void setProducts(Map<String, Product> products) {
        this.products = products;
    }

    public void add(Product product){
        String pid = product.getPid();
        if(products.containsKey(pid)){
            keys.remove(pid);
            keys.addFirst(pid);
        }
        products.put(pid,product);

        if(keys.size() >= COUNTSIZE){
            String key = keys.removeLast();
            products.remove(key);
        }
        keys.addFirst(pid);
    }
}
